package org.david.code.util;

import java.sql.DatabaseMetaData;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.log4j.Logger;

public class DBMetaDataFactory {
	Logger logger = Logger.getLogger(DBMetaDataFactory.class);
	private DataSource dataSource;
	private  DatabaseMetaData dbMetaData;
	private DBMetaDataFactory(){
		
	}
	public DataSource getDataSource() {
		return dataSource;
	}
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	/**
	 * 根据dataSource创建DataBaseMetaData
	 * @param dataSource
	 * @return
	 * @throws SQLException
	 */
	public  DatabaseMetaData create() throws SQLException{
		if(dataSource==null){
			logger.error("dataSource is null init DataBaseMetaData failure");
			return null;
		}
		if(dbMetaData!=null){
			return dbMetaData;
		}
		return dataSource.getConnection().getMetaData(); 
		
	}
}
